// jquery的页面加载函数
$(function() {
    var loadindex = layer.load(1, {
        shade: [0.5, '#333']
    })
    var username = getCookie('username')
    var login = document.querySelector(".login");
    if (username) {
        var str = `
        <li><a href="">欢太商城APP</a></li>
        <li><a href="javascript:;"><i class="iconfont icon-yonghu"></i><span>${username}</span>
            </a></li>
            <li><a href="javascript:;" class="logout">退出</a></li>
        <li><a href="../html/cart.html"><i class="iconfont icon-gouwuchekong"></i><span>购物车</span><span class="qwe">(0)</span></a></li>
        `
        login.innerHTML = str;
        // 退出功能
        var logout = document.querySelector(".logout");
        logout.onclick = function() {
            layer.confirm('你确定要退出吗？', {
                    btn: ['确定', '取消'] //按钮
                },
                function() {
                    // 删除cookie
                    delCookie('username');
                    login.innerHTML = `
                    <li><a href="">欢太商城APP</a></li>
                    <li><a href="./login.html"><i class="iconfont icon-yonghu"></i><span>登录</span>
                        </a></li>
                    <li><a href="./register.html">注册</a></li>
                    <li><a href="" class="num"><i class="iconfont icon-gouwuchekong"></i><span>购物车</span><span>(0)</span></a></li>
                    `
                    var msgindex = layer.msg("退出成功", {
                        icon: 1
                    })
                    setTimeout(function() {
                        layer.close(msgindex)
                        location.href = '../html/list.html';
                    }, 2000)
                },
                function() {
                    layer.msg("已取消", {
                        icon: 1,
                        time: 500
                    })
                    return false;
                }
            );
        }
    }

    $.ajax({
            url: '../server/list.php',
            dataType: 'json',
            success(res) {
                //   获取到所有景点的信息
                var data = res.data;
                //   console.log(data);
                // 每一页给四个数据
                var pageSize = 8;
                new Page("page", {
                    language: {
                        first: '首页',
                        prev: '上一页',
                        next: '下一页',
                        last: '尾页'
                    },
                    pageData: {
                        pageSize,
                        total: data.length
                    },
                    show: function(currentPage) {
                        var tmp = data.slice((currentPage - 1) * pageSize, currentPage * pageSize)
                        var html = '';
                        tmp.forEach(v => {
                            html += `
                            <div class="title">
                                <img src="${v.imgpath}" alt="">
                                <p>${v.name}</p>
                                <li><b>${v.b}</b></li>
                                <span>￥${v.price}<s>${v.s}</s></span>
                                <a href="../html/detail.html?id=${v.id}">查看详情 </a>
                            </div>
                                        `
                        })
                        $('.content aside').html(html)
                    }
                })
                layer.close(loadindex)
            }
        })
        // ***********************************
    function getNum() {
        var data = localStorage.getItem('data');

        var trr = JSON.parse(data);
        var trr = trr.map(v => {
            if (v.username === username) {
                return v.num
            }
        });
        // alert(trr)
        // console.log(trr);
        var alll = 0;
        for (var i = 0; i < trr.length; i++) {
            alll += parseInt(trr[i])
        }
        // alert($('.login .qwe').text())
        //获取购物车的数量
        $('.login .qwe').text("(" + alll + ")")
    }
    getNum();
})
async function getPeo() {
    var peo = document.querySelector('.bottom');
    var peo_list = await pAjax({
        url: '../server/list_banner.php',
        data: { pid: 6 }
    });
    var { data } = peo_list;
    console.log(data)
    var str = '';
    data.forEach(function(item) {
        str += `
            <div>
            <img src="${item.imgpath}" alt="">
            <span>${item.name}</span>
        </div>
        `
    })
    peo.innerHTML = str;
}

getPeo();